package com.ww.microrpc.netty;

import com.ww.microrpc.netty.handler.client.SendCallback;
import com.ww.microrpc.netty.protocol.Message;
import com.ww.microrpc.netty.protocol.RequestMessage;

/**
 * RPC Request
 *
 * @author ww
 */
public class RequestHolder implements Message {
    private static final long serialVersionUID = -2524587347775862771L;
    /**
     * 服务名称
     */
    private String clientName;

    /**
     * 服务地址 示例 127.0.0.0:8888
     */
    private String url;

    /**
     * 请求ID
     */
    private int requestId;

    /**
     * 类名
     */
    private String className;

    /**
     * 方法名
     */
    private String methodName;

    /**
     * 参数类型列表
     */
    private Class<?>[] parameterTypes;

    /**
     * 参数列表
     */
    private Object[] parameters;

    /**
     * 序列化方式
     */
    private int serializer;

    /**
     * 异步回调
     */
    private transient SendCallback callback;

    private Class<?> returnType;

    /**
     * 是否重试
     */
    private boolean retriable;

    public Class<?> getReturnType() {
        return returnType;
    }

    public void setReturnType(Class<?> returnType) {
        this.returnType = returnType;
    }

    public void setSerializer(int serializer) {
        this.serializer = serializer;
    }

    public int getSerializer() {
        return serializer;
    }

    @Override
    public int getSerializerType() {
        return serializer;
    }

    public boolean isAsync() {
        return this.callback != null;
    }
    public boolean isSync() {
        return this.callback == null;
    }

    public SendCallback getCallback() {
        return callback;
    }

    public void setCallback(SendCallback callback) {
        this.callback = callback;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isRetriable() {
        return retriable;
    }

    public void setRetriable(boolean retriable) {
        this.retriable = retriable;
    }

    public int getRequestId() {
        return requestId;
    }

    public void setRequestId(int requestId) {
        this.requestId = requestId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Class<?>[] getParameterTypes() {
        return parameterTypes;
    }

    public void setParameterTypes(Class<?>[] parameterTypes) {
        this.parameterTypes = parameterTypes;
    }

    public Object[] getParameters() {
        return parameters;
    }

    public void setParameters(Object[] parameters) {
        this.parameters = parameters;
    }

    @Override
    public int getMessageType() {
        return Constant.MessageType.RpcRequest;
    }

    @Override
    public String toString() {
        return "RpcRequest{" +
                "clientName='" + clientName + '\'' +
                ", url='" + url + '\'' +
                ", requestId='" + requestId + '\'' +
                ", className='" + className + '\'' +
                ", methodName='" + methodName + '\'' +
                '}';
    }

    public RequestMessage getRequestMessage() {
        return new RequestMessage(this.requestId,
                this.className,
                this.methodName,
                this.parameterTypes,
                this.parameters);
    }

}